package calcolatrice.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class CalcolatriceGui {

	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.setResizable(false);
		f.setLayout(null);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		f.setSize(342, 410);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setTitle("Calcolatrice");
		f.setVisible(true);

		JTextField tx = new JTextField();
		tx.setSize(317, 40);
		tx.setLocation((screenSize.width / 2) - (f.getWidth() / 2),
				(screenSize.height / 2) - (f.getHeight() / 2));
		tx.setHorizontalAlignment(SwingConstants.RIGHT);
		tx.setEditable(false);
		tx.setBackground(Color.WHITE);
		f.add(tx);

		JButton b0 = new JButton();
		b0.setSize(77, 77);
		b0.setLocation(3, 283);
		b0.setText("0");
		f.add(b0);

		JButton b1 = new JButton();
		b1.setSize(77, 77);
		b1.setLocation(3, 203);
		b1.setText("1");
		f.add(b1);

		JButton b2 = new JButton();
		b2.setSize(77, 77);
		b2.setLocation(83, 203);
		b2.setText("2");
		f.add(b2);

		JButton b3 = new JButton();
		b3.setSize(77, 77);
		b3.setLocation(163, 203);
		b3.setText("3");
		f.add(b3);

		JButton b4 = new JButton();
		b4.setSize(77, 77);
		b4.setLocation(3, 123);
		b4.setText("4");
		f.add(b4);

		JButton b5 = new JButton();
		b5.setSize(77, 77);
		b5.setLocation(83, 123);
		b5.setText("5");
		f.add(b5);

		JButton b6 = new JButton();
		b6.setSize(77, 77);
		b6.setLocation(163, 123);
		b6.setText("6");
		f.add(b6);

		JButton b7 = new JButton();
		b7.setSize(77, 77);
		b7.setLocation(3, 43);
		b7.setText("7");
		f.add(b7);

		JButton b8 = new JButton();
		b8.setSize(77, 77);
		b8.setLocation(83, 43);
		b8.setText("8");
		f.add(b8);

		JButton b9 = new JButton();
		b9.setSize(77, 77);
		b9.setLocation(163, 43);
		b9.setText("9");
		f.add(b9);

		JButton dividi = new JButton();
		dividi.setSize(77, 77);
		dividi.setLocation(243, 43);
		dividi.setText("/");
		f.add(dividi);

		JButton moltiplica = new JButton();
		moltiplica.setSize(77, 77);
		moltiplica.setLocation(243, 123);
		moltiplica.setText("*");
		f.add(moltiplica);

		JButton sottrai = new JButton();
		sottrai.setLayout(null);
		sottrai.setSize(77, 77);
		sottrai.setLocation(243, 203);
		sottrai.setText("-");
		sottrai.setVisible(true);
		f.add(sottrai);

		JButton somma = new JButton();
		somma.setSize(77, 77);
		somma.setLocation(243, 283);
		somma.setText("+");
		f.add(somma);

		JButton risultato = new JButton();
		risultato.setSize(77, 77);
		risultato.setLocation(83, 283);
		risultato.setText("=");
		f.add(risultato);

		JButton pulisci = new JButton();
		pulisci.setSize(77, 77);
		pulisci.setLocation(163, 283);
		pulisci.setText("CE");
		f.add(pulisci);

		JPanel p = new JPanel();
		p.setVisible(true);
		f.add(p);
		b0 gest0 = new b0(tx, b0);
		b0.addActionListener(gest0);

		b1 gest1 = new b1(tx, b1);
		b1.addActionListener(gest1);

		b2 gest2 = new b2(tx, b2);
		b2.addActionListener(gest2);

		b3 gest3 = new b3(tx, b3);
		b3.addActionListener(gest3);

		b4 gest4 = new b4(tx, b4);
		b4.addActionListener(gest4);

		b5 gest5 = new b5(tx, b5);
		b5.addActionListener(gest5);

		b6 gest6 = new b6(tx, b6);
		b6.addActionListener(gest6);

		b7 gest7 = new b7(tx, b7);
		b7.addActionListener(gest7);

		b8 gest8 = new b8(tx, b8);
		b8.addActionListener(gest8);

		b9 gest9 = new b9(tx, b9);
		b9.addActionListener(gest9);

		dividi gest10 = new dividi(tx, dividi);
		dividi.addActionListener(gest10);

		moltiplica gest11 = new moltiplica(tx, moltiplica);
		moltiplica.addActionListener(gest11);

		sottrai gest12 = new sottrai(tx, sottrai);
		sottrai.addActionListener(gest12);

		somma gest13 = new somma(tx, somma);
		somma.addActionListener(gest13);

		risultato gest14 = new risultato(tx, risultato);
		risultato.addActionListener(gest14);

		pulisci gest15 = new pulisci(tx, pulisci);
		pulisci.addActionListener(gest15);

	}
}

class b0 implements ActionListener {
	JTextField tx;
	JButton b0;

	public b0(JTextField tx, JButton b0) {
		this.tx = tx;
		this.b0 = b0;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "0");
	}
}

class b1 implements ActionListener {
	JTextField tx;
	JButton b1;

	public b1(JTextField tx, JButton b1) {
		this.tx = tx;
		this.b1 = b1;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "1");
	}
}

class b2 implements ActionListener {
	JTextField tx;
	JButton b2;

	public b2(JTextField tx, JButton b2) {
		this.tx = tx;
		this.b2 = b2;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "2");
	}
}

class b3 implements ActionListener {
	JTextField tx;
	JButton b3;

	public b3(JTextField tx, JButton b3) {
		this.tx = tx;
		this.b3 = b3;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "3");
	}
}

class b4 implements ActionListener {
	JTextField tx;
	JButton b4;

	public b4(JTextField tx, JButton b4) {
		this.tx = tx;
		this.b4 = b4;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "4");
	}
}

class b5 implements ActionListener {
	JTextField tx;
	JButton b5;

	public b5(JTextField tx, JButton b5) {
		this.tx = tx;
		this.b5 = b5;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "5");
	}
}

class b6 implements ActionListener {
	JTextField tx;
	JButton b6;

	public b6(JTextField tx, JButton b6) {
		this.tx = tx;
		this.b6 = b6;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "6");
	}
}

class b7 implements ActionListener {
	JTextField tx;
	JButton b7;

	public b7(JTextField tx, JButton b7) {
		this.tx = tx;
		this.b7 = b7;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "7");
	}
}

class b8 implements ActionListener {
	JTextField tx;
	JButton b8;

	public b8(JTextField tx, JButton b8) {
		this.tx = tx;
		this.b8 = b8;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "8");
	}
}

class b9 implements ActionListener {
	JTextField tx;
	JButton b9;

	public b9(JTextField tx, JButton b9) {
		this.tx = tx;
		this.b9 = b9;
	}

	public void actionPerformed(ActionEvent e) {
		String x = tx.getText();
		tx.setText(x + "9");
	}
}

class dividi extends var implements ActionListener {
	JTextField tx;
	JButton dividi;

	public dividi(JTextField tx, JButton dividi) {
		this.tx = tx;
		this.dividi = dividi;
	}

	public void actionPerformed(ActionEvent e) {
		primo = Double.parseDouble(tx.getText());
		tx.setText("");
		scelta = "/";

	}
}

class moltiplica extends var implements ActionListener {
	private JTextField tx;
	JButton moltiplica;

	public moltiplica(JTextField tx, JButton moltiplica) {
		this.tx = tx;
		this.moltiplica = moltiplica;
	}

	public void actionPerformed(ActionEvent e) {
		primo = Double.parseDouble(tx.getText());
		tx.setText("");
		scelta = "*";

	}
}

class sottrai extends var implements ActionListener {
	JTextField tx;
	JButton sottrai;

	public sottrai(JTextField tx, JButton sottrai) {
		this.tx = tx;
		this.sottrai = sottrai;
	}

	public void actionPerformed(ActionEvent e) {
		primo = Double.parseDouble(tx.getText());
		tx.setText("");
		scelta = "-";

	}
}

class somma extends var implements ActionListener {
	JTextField tx;
	JButton somma;

	public somma(JTextField tx, JButton somma) {
		this.tx = tx;
		this.somma = somma;
	}

	public void actionPerformed(ActionEvent e) {
		primo = Double.parseDouble(tx.getText());
		tx.setText("");
		scelta = "+";

	}
}

class pulisci implements ActionListener {
	JTextField tx;
	JButton pulisci;

	public pulisci(JTextField tx, JButton pulisci) {
		this.tx = tx;
		this.pulisci = pulisci;
	}

	public void actionPerformed(ActionEvent e) {
		tx.setText("");

	}
}

class risultato extends var implements ActionListener {
	JTextField tx;
	JButton risultato;
	double ris;

	public risultato(JTextField tx, JButton risultato) {
		this.tx = tx;
		this.risultato = risultato;
	}

	public void actionPerformed(ActionEvent e) {
		secondo = Double.parseDouble(tx.getText());
		tx.setText("");
		if (scelta.equals("/")) {
			tx.setText(primo / secondo + "");
		}
		if (scelta.equals("*")) {
			tx.setText(primo * secondo + "");
		}
		if (scelta.equals("-")) {
			tx.setText(primo - secondo + "");
		}
		if (scelta.equals("+")) {
			tx.setText(primo + secondo + "");
		}
	}
}